package com.kzw.web;

import org.apache.commons.lang3.StringUtils;

import com.kzw.entity.Category;
import com.kzw.service.CategoryService;

import my.bean.Msg;
import my.db.Page;
import my.mvc.Annotation.JSON;
import my.mvc.RequestContext;

@SuppressWarnings("unchecked")
public class CategoryAction {

	private CategoryService service = new CategoryService();

	/**
	 * 列表
	 */
	@JSON
	public Page<Category> list(RequestContext rc) {
		// 获得分页信息, layui表格分页参数：page、limit
		Page<Category> page = rc.form(Page.class);
		String name = rc.param("name");
		service.list(page, name);
		return page;
	}

	/**
	 * 保存
	 */
	@JSON
	public Msg save(RequestContext rc) throws Exception {
		Category a = rc.form(Category.class);
		if (a.getId() == 0) {
			service.save(a);
		} else {
			service.update(a);
		}

		return new Msg(true);
	}

	/**
	 * 获得
	 */
	public void get(RequestContext rc) {
		long id = rc.param("id", 0L);
		Category category = service.get(id);
		rc.reqAttr("category", category);
		rc.forward("/page/blog/category_form.jsp");
	}
	
	/**
	 * 删除
	 * */
	@JSON
	public Msg multiDel(RequestContext rc) {
		String ids = rc.param("ids");
		if(StringUtils.isNotBlank(ids)) {
			String[] arr = ids.split(",");
			for(String id : arr) {
				service.delete(Long.parseLong(id));
			}
		}
		return new Msg();
	}
}
